
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





PAL[#](#pal "跳转到此标题的链接")
========================

实现了程序辅助的语言模型，例如 https://arxiv.org/pdf/2211.10435.pdf。

```python
from langchain.chains import PALChain
from langchain import OpenAI

```

```python
llm = OpenAI(temperature=0, max_tokens=512)

```

数学提示[#](#math-prompt "跳转到此标题的链接")
---------------------------------

```python
pal_chain = PALChain.from_math_prompt(llm, verbose=True)

```

```python
question = "Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?"

```

```python
pal_chain.run(question)

```

```python
> Entering new PALChain chain...
def solution():
 """Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?"""
 cindy_pets = 4
 marcia_pets = cindy_pets + 2
 jan_pets = marcia_pets * 3
 total_pets = cindy_pets + marcia_pets + jan_pets
 result = total_pets
 return result

> Finished chain.

```

```python
'28'

```

彩色物体[#](#colored-objects "跳转到此标题的链接")
-------------------------------------

```python
pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)

```

```python
question = "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"

```

```python
pal_chain.run(question)

```

```python
> Entering new PALChain chain...
# Put objects into a list to record ordering
objects = []
objects += [('booklet', 'blue')] * 2
objects += [('booklet', 'purple')] * 2
objects += [('sunglasses', 'yellow')] * 2

# Remove all pairs of sunglasses
objects = [object for object in objects if object[0] != 'sunglasses']

# Count number of purple objects
num_purple = len([object for object in objects if object[1] == 'purple'])
answer = num_purple

> Finished PALChain chain.

```

```python
'2'

```

中间步骤[#](#intermediate-steps "跳转到此标题的链接")
----------------------------------------

你还可以使用中间步骤标志来返回生成答案的代码。

```python
pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True, return_intermediate_steps=True)

```

```python
question = "On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"

```

```python
result = pal_chain({"question": question})

```

```python
> Entering new PALChain chain...
# Put objects into a list to record ordering
objects = []
objects += [('booklet', 'blue')] * 2
objects += [('booklet', 'purple')] * 2
objects += [('sunglasses', 'yellow')] * 2

# Remove all pairs of sunglasses
objects = [object for object in objects if object[0] != 'sunglasses']

# Count number of purple objects
num_purple = len([object for object in objects if object[1] == 'purple'])
answer = num_purple

> Finished chain.

```

```python
result['intermediate_steps']

```

```python
"# Put objects into a list to record ordering\nobjects = []\nobjects += [('booklet', 'blue')] * 2\nobjects += [('booklet', 'purple')] * 2\nobjects += [('sunglasses', 'yellow')] * 2  # Remove all pairs of sunglasses\nobjects = [object for object in objects if object[0] != 'sunglasses']  # Count number of purple objects\nnum_purple = len([object for object in objects if object[1] == 'purple'])\nanswer = num_purple"

```

